Performance for myCollection.Add() vs. myCollection["key"]

Posted by Atomiton on Stack Overflow See other posts from Stack Overflow or by Atomiton
Published on 2010-03-24T16:23:40Z Indexed on 2010/03/24 16:33 UTC
Read the original article Hit count: 406

When dealing with a collection of key/value pairs is there any difference between using its Add() method and directly assigning it?

For example, a HtmlGenericControl will have an Attributes Collection:

var anchor = new HtmlGenericControl("a");

// These both work:
anchor.Attributes.Add("class", "xyz");
anchor.Attributes["class"] = "xyz";

Is it purely a matter of preference, or is there a reason for doing one or the other?

© Stack Overflow or respective owner

Related posts about c#

Related posts about collections